package com.saasitems.admin.service;

import com.saasitems.admin.dao.PartsTypeMapper;
import com.saasitems.admin.model.PartsType;
import com.saasitems.admin.model.PartsTypeExample;
import com.saasitems.admin.web.vo.partstype.TreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/12/16.
 */
@Service
public class PartsTypeService {

    @Autowired
    private PartsTypeMapper partsTypeMapper;

    /**
     * 取得表中树状结构
     * @return
     */
    public List<TreeVO> getTree() {
        List<TreeVO> ret = null;
        PartsTypeExample ex = new PartsTypeExample();
        ex.createCriteria()
            .andFIdEqualTo(0L)
            .andFlagEqualTo(0);
        ex.setOrderByClause("sort asc");
        List<PartsType> list = partsTypeMapper.selectByExample(ex);
        if (list != null && !list.isEmpty()) {
            ret = new ArrayList<TreeVO>();

            for (PartsType pt : list) {
                TreeVO vo = cpBean(pt);
                // 第一级
                vo.setLevel(1);
                List<TreeVO> sub = getSubTree(pt.getId(), 2);
                vo.setChildren(sub);
                // 添加第一层
                ret.add(vo);
            }
        }
        return ret;
    }

    /**
     * 递归取子类数据
     * @param fId
     * @return
     */
    public List<TreeVO> getSubTree(Long fId, int level) {
        List<TreeVO> ret = null;
        PartsTypeExample ex = new PartsTypeExample();
        ex
                .createCriteria()
                .andFIdEqualTo(fId)
                .andFlagEqualTo(0);
        ex.setOrderByClause("sort asc");
        List<PartsType> list = partsTypeMapper.selectByExample(ex);

        if (list != null && !list.isEmpty()) {
            ret = new ArrayList<TreeVO>();
            for (PartsType pt : list) {
                TreeVO vo = cpBean(pt);
                vo.setLevel(level);
                List<TreeVO> sub = getSubTree(pt.getId(), level + 1);
                vo.setChildren(sub);
                // 添加子类
                ret.add(vo);
            }
        }
        return ret;
    }

    public TreeVO cpBean(PartsType pt) {
        TreeVO vo = new TreeVO();
        vo.setId(pt.getId());
        vo.setTypeCode(pt.getTypeCode());
        vo.setTypeName(pt.getTypeName());
        vo.setTypeNameEn(pt.getTypeNameEn());
        vo.setSort(pt.getSort());
        vo.setfId(pt.getfId());
        return vo;
    }

    public void insert(PartsType vo) {
//        PartsType pt = new PartsType();
//        pt.setTypeCode("00001");
//        pt.setTypeName("测试");
//        pt.setTypeNameEn("ceshi");
//        pt.setfId((long) 0);
//        pt.setEpId((long) 0);
//        pt.setDepartmentId((long) 0);
//        pt.setCreatetime(new Date());
//        pt.setFlag(0);
        partsTypeMapper.insert(vo);
    }

    public PartsType getInfo(Long id) {
        return this.partsTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新
     * @param vo
     */
    public int update(PartsType vo) {

        return partsTypeMapper.updateByPrimaryKeySelective(vo);
    }
}
